<?php
 /***************************************************************
 * Mor-MVC Framework, A framework that can easily let you build a powerful
 * and scalable web application based on PHP language,
 * this framewrok is a copy of the Zend Behavior Framework
 *  Copyright (C) 2010  Nabil Droussi <nabildroussi@gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ***************************************************************/
class Utility
{
    public static function convertToArray($objArray)
    {
        if(!is_array($objArray))
            throw new Exception("The parameter is not an array of obects.");
        $rootArray = array();
        foreach($objArray as $object)
        {
            if(!is_object($object))
                throw new Exception("All the array elements must be objects.");

            $objVars = get_object_vars($object);
            $rootArray[] = $objVars;
        }
        return $rootArray;
    }
    public static function writeLog($str)
    {
        $fp = fopen("log.txt","a+");
        fseek($fp, filesize("log.txt")-1);
        fwrite($fp,date("d/m/Y H:i:s")." : ".$str);
        fclose($fp);
    }
    public static function generateKey()
    {
        $strArray = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        $keys = array(7,4,6,15);
        $key = "";
        for($i=0;$i<count($keys);$i++)
        {
            for($j=0;$j<$keys[$i];$j++)
            {
                $key .= $strArray[rand(0,  strlen($strArray)-1)];
            }
            if(($i+1) != count($keys))
                $key .= "-";
        }
        return $key;
    }
}
